﻿using QingZhouLinYe_Web.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Threading.Tasks;
namespace QingZhouLinYe_Web.DbContext
{
    public class DbServices
    {

        public List<FangHuoDanWei> GetFangHuoDanWeisByQuHuaId(int qhid)
        {
            using (SqlSugarClient db = DbHelper.GetDb())
            {
                db.Ado.BeginTran();
                var fhdws = db.Queryable<FangHuoDanWei>()
                    .Includes(fhdw => fhdw.QuHua)
                    .ToList();
                foreach (FangHuoDanWei f in fhdws)
                {
                    Debug.WriteLine("ssss" + f.CenterShape.ToString());
                }
                return fhdws;
            }
        }

        public List<SheBei> GetSheBeis(int authority,int fhdwid,int sblxid)
        {
            using (SqlSugarClient db= DbHelper.GetDb()){
                try
                {
                    db.Ado.BeginTran();
                    Debug.WriteLine("开启事务：");
                    if (authority == 0)
                    {
                        var sbs = db.Queryable<SheBei>()
                        .Includes(sb => sb.FangHuoDanWei)
                        .Includes(sb => sb.SheBeiLeiXing)
                        .Where(sb => sb.LeiXingId == sblxid)
                        .ToList();
                        db.Ado.CommitTran();
                        Debug.WriteLine("超级权限完成事务：");
                        if (sbs.Count > 0) { return sbs; }
                    }
                    else if (authority == 1)
                    {
                        var sbs = db.Queryable<SheBei>()
                            .Includes(sb => sb.FangHuoDanWei)
                            .Includes(sb => sb.SheBeiLeiXing)
                            .Where(sb => sb.FangHuoDanWeiId == fhdwid)
                            .Where(sb => sb.LeiXingId == sblxid)
                            .ToList();
                        db.Ado.CommitTran();
                        Debug.WriteLine("管理员权限完成事务：");
                        if (sbs.Count > 0) { return sbs; }
                    }
                    else if (authority == 2)
                    {
                        var sbs = db.Queryable<SheBei>()
                            .Includes(sb => sb.FangHuoDanWei)
                            .Includes(sb => sb.SheBeiLeiXing)
                            .Where(sb => sb.FangHuoDanWeiId == fhdwid)
                            .Where(sb => sb.LeiXingId == sblxid)
                           .ToList();
                        db.Ado.CommitTran();
                        Debug.WriteLine("普通权限完成事务：");
                        if (sbs.Count > 0) { return sbs; }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("错误事务：" + ex);
                    db.Ado.RollbackTran();
                }
                return null;
            }
        }

        public List<FangHuoDanWei> GetFangHuoDanWeisForSheBei(int fhdwid)
        {
            using (SqlSugarClient db = DbHelper.GetDb())
            {
                db.Ado.BeginTran();
                var fhdws = db.Queryable<FangHuoDanWei>()
                    .Includes(sb => sb.SheBeis)
                    .Includes(sb => sb.QuHua)
                    .ToList();
                foreach (FangHuoDanWei f in fhdws)
                {
                    Debug.WriteLine("ssss" + f.CenterShape.ToString());
                }
                return fhdws;
            }
        }

        public List<SheBei> GetSheBeis()
        {
            using (SqlSugarClient db = DbHelper.GetDb())
            {
                db.Ado.BeginTran();
                var sbs = db.Queryable<SheBei>()
                    .Includes(sb => sb.SheBeiLeiXing)
                    .Includes(sb => sb.FangHuoDanWei)
                    .ToList();
                return sbs;
            }
        }

        public List<QuHua> getQuHuaNav(int id)
        {
            using (SqlSugarClient db = DbHelper.GetDb())
            {
                db.Ado.BeginTran();
                var qhs = db.Queryable<QuHua>()
                    .Includes(qh => qh.FangHuoDanWeis)
                    .Where(qh => qh.Id == id)
                    .ToList();
                return qhs;
            }
        }

        public List<QuHua> GetQuHuas(int qhid)
        {
            using (SqlSugarClient db = DbHelper.GetDb())
            {
                db.Ado.BeginTran();
                var qhs = db.Queryable<QuHua>()
                    .ToList();
                return qhs;
            }
        }

        public List<QuHua> getQuHuaTree()
        {
            using (SqlSugarClient db = DbHelper.GetDb())
            {

                //var qhRoot = db.Queryable<QuHua>().Where(it => it.Id == 1).ToList();
                ////第一次
                //db.ThenMapper(qhRoot, item =>
                //{
                //    item.Child = db.Queryable<QuHua>().SetContext(x => x.UpId, () => item.Id, item).ToList();
                //});
                ////第二次 
                //db.ThenMapper(qhRoot.SelectMany(it => it.Child), it =>
                //{
                //    it.Child = db.Queryable<QuHua>().SetContext(x => x.UpId,() => it.Id,it).ToList();
                //});
                //// 第三次
                //db.ThenMapper(qhRoot.SelectMany(it => it.Child).SelectMany(it => it.Child), it =>
                //{
                //    it.Child = db.Queryable<QuHua>().SetContext(x => x.UpId,() =>it.Id,it).ToList();
                //});

                //查询所有下级
                List<QuHua> list = db.Queryable<QuHua>().ToTree(it => it.Child, it => it.UpId, 0);  //最后的1表示出现UpId为1开始的下级目录

                //查询所有上级
                //List<QuHua> uplist = db.Queryable<QuHua>().ToParentList(it => it.UpId, 9).ToList();

                //条件查询；不可用
                //List<QuHua> list = db.Queryable<QuHua>().Where(it => it.Id == 3).ToTree(it => it.Child, it => it.UpId, 0);  //出现为空
                return list;
            }
        }

        public int saveQuHua(QuHua qh)
        {
            using (SqlSugarClient db = DbHelper.GetDb())
            {
                return db.Insertable(qh).ExecuteReturnIdentity();
            }
        }

        public int saveFangHuoDanWei(FangHuoDanWei fhdw)
        {
            using (SqlSugarClient db = DbHelper.GetDb())
            {
                return db.Insertable(fhdw).ExecuteReturnIdentity();
            }
        }

        #region
        
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">登录密码</param>
        public UserInfo userLogin(string username,string password)
        {
            using(SqlSugarClient db = DbHelper.GetDb())
            {
                var user = db.Queryable<UserInfo>()
                    .Includes(ui => ui.FangHuoDanWei)
                    .Where(ui => ui.Name == username && ui.PassWord == password)
                    .Single();
                return user;
            }
        }

        /// <summary>
        /// 根据用户Id获取用户信息
        /// </summary>
        /// <param name="Id">用户Id</param>
        /// <returns></returns>
        public UserInfo getUser(int Id)
        {
            using (SqlSugarClient db = DbHelper.GetDb())
            {
                var user = db.Queryable<UserInfo>()
                    .LeftJoin<FangHuoDanWei>((ui,fhdw) => ui.FangHuoDanWeiId == fhdw.Id)
                    .Where(ui => ui.Id == Id)
                    .Select((ui,fhdw) => new UserInfo
                    {
                        Id = ui.Id,
                        Name = ui.Name,
                        PassWord = ui.PassWord,
                        FangHuoDanWeiId = ui.FangHuoDanWeiId,
                        Authority = ui.Authority,
                        FangHuoDanWei = fhdw
                    })
                    .Single();
                return user;
            }
        }

        #endregion


        #region  火情
        public List<HuoQing> getHuoQingByZhuangTaiId(int ztid, int fhdwid, int authority)
        {
            
            using (SqlSugarClient db = DbHelper.GetDb())
            {
                List<HuoQing> hqs = new List<HuoQing>();
                try
                {
                    db.Ado.BeginTran();
                    if (authority == 0)
                    {
                        hqs = db.Queryable<HuoQing>()
                         .Includes(hq => hq.HuoQingLeiBie)
                         .Includes(hq => hq.HuoQingZhiBei)
                         .Includes(hq => hq.FangHuoDanWei)
                         .Includes(hq => hq.SheBei)
                         .Includes(hq => hq.HuoQingZhuangTai)
                         .Where(hq => hq.HuoQingZhuangTaiId == ztid)
                         .ToList();
                        db.Ado.CommitTran();
                    }
                    else if (authority == 1 || authority == 2)
                    {
                        hqs = db.Queryable<HuoQing>()
                           .Includes(hq => hq.HuoQingLeiBie)
                           .Includes(hq => hq.HuoQingZhiBei)
                           .Includes(hq => hq.FangHuoDanWei)
                           .Includes(hq => hq.SheBei)
                           .Includes(hq => hq.HuoQingZhuangTai)
                           .Where(hq => hq.HuoQingZhuangTaiId == ztid)
                           .Where(hq => hq.FangHuoDanWeiId == fhdwid)
                           .ToList();
                        db.Ado.CommitTran();
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("火情事务：" + ex);
                    db.Ado.RollbackTran();

                }
                if (hqs.Count > 0) {
                    return hqs;
                }
                return null;
            }
        }

        public int updateHuoQingZhuangTai(int ztid,int hqid)
        {
            using (SqlSugarClient db = DbHelper.GetDb())
            {
                var hqrow = -1;
                try
                {
                    db.Ado.BeginTran();
                    Debug.WriteLine("火情更新事务开始");
                    hqrow = db.Updateable<HuoQing>()
                        .SetColumns(hq => hq.HuoQingZhuangTaiId == ztid)
                        .Where(hq => hq.Id == hqid)
                        .ExecuteCommand();
                    db.Ado.CommitTran();
                    Debug.WriteLine("火情更新事务结束");
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("火情更新事务报错：" + ex);
                    db.Ado.RollbackTran();

                }
                return hqrow;
            }
        }
        #endregion
    }
}